/**
 *Copyright (c) 2024 watereyes
 * safetymanagement is licensed under Mulan PubL v2.
 * You can use this software according to the terms and conditions of the Mulan
 * PubL v2.
 * You may obtain a copy of Mulan PubL v2 at:
 *          http://license.coscl.org.cn/MulanPubL-2.0
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PubL v2 for more details.
 */
package com.koron.report.service.impl;

import com.koron.accident.mapper.AccidentReportMapper;
import com.koron.accident.mapper.EventMapper;
import com.koron.common.bean.DepartmentTreeBean;
import com.koron.common.web.Util;
import com.koron.report.bean.EventAnalysis;
import com.koron.report.mapper.EventDataAnalysisMapper;
import com.koron.report.service.api.EventAnalysisService;
import com.koron.report.util.BusinessCode;
import com.koron.risk.bean.CorpBean;
import com.koron.risk.mapper.CorpManagementMapper;
import com.koron.risk.service.api.CorpManagementService;
import com.koron.risk.service.api.GridNodeService;
import com.koron.system.domain.DictData;
import com.koron.system.domain.District;
import com.koron.system.domain.DistrictCorpRelation;
import com.koron.system.mapper.DictDataMapper;
import com.koron.system.mapper.DistrictMapper;
import com.koron.system.service.api.DistrictService;
import org.apache.commons.lang3.StringUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.swan.bean.MessageBean;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.time.*;
import java.util.*;
import java.util.stream.Collectors;

@Service
public class EventAnalysisServiceImpl implements EventAnalysisService {

    private final static  Logger logger= LoggerFactory.getLogger(EventAnalysisServiceImpl.class);

    @Autowired
    private Util util;

    @Autowired
    private DistrictService districtService;

    @Autowired
    private CorpManagementService corpManagementService;

    private List<String> nodeList= Arrays.asList("0f92c31a7bdf4199aef13f1525e80f88","2977d7c012e74b8e91a4a7ae16e0d65e",
            "a961752329aa4614ad4d78e53b28f76b","e137bf5392ae48859429012500f6d2d0","e5919a66cedc4067bb1f9218d648d72d");

    @Autowired
    private GridNodeService nodeService;

    @Override
    public MessageBean<?> queryCountEvent(String code, Integer year) {
        MessageBean<Map> msg=MessageBean.create(BusinessCode.SUCCESS.getCode(),BusinessCode.SUCCESS.getDescription(),Map.class);
        try(SessionFactory factory=new SessionFactory()){
            if (null==year){
                msg.setCode(BusinessCode.EMPTY_PARAM_REQUIRED.getCode());
                msg.setDescription(BusinessCode.EMPTY_PARAM_REQUIRED.getDescription());
                return msg;
            }
            List<DictData> ddlist = factory.getMapper(DictDataMapper.class).queryAll(null,"incident_type", null, null);
            if (null==ddlist||ddlist.size()<=0){
                return msg;
            }
            if (StringUtils.isEmpty(code)){
                //查询整个集团的事件按类型统计情况
                EventDataAnalysisMapper dataAnalysisMapper=factory.getMapper(EventDataAnalysisMapper.class);
                LinkedHashMap<String,Object> map=dataAnalysisMapper.queryCountEventForCorp(ddlist,null,getBeginTime(year),getEndTime(year));
                if (map==null||!map.isEmpty()){
                    map=new LinkedHashMap<>();
                }
                map.put("集团","集团");
                msg.setData(map);
            }else{
                //统计某片区或者某个水司
                District district=factory.getMapper(DistrictMapper.class).queryOneDistrictByCode(code);
                CorpBean corpBean=factory.getMapper(CorpManagementMapper.class).get(code);
                if (null==district&&null==corpBean){
                    msg.setCode(BusinessCode.SUCCESS.getCode());
                    msg.setDescription(BusinessCode.SUCCESS.getDescription());
                    return msg;
                }
                if (null!=district){
                    List<String> codeList=new ArrayList<>();
                    List<DistrictCorpRelation> relationList=factory.getMapper(DistrictMapper.class).queryDistrictCorp(code,null,null);
                    if (null!=relationList&&relationList.size()>0)
                        codeList=relationList.stream().map(DistrictCorpRelation::getCorpCode).collect(Collectors.toList());
                    if (null!=codeList&&codeList.size()>0){
                        LinkedHashMap<String,Object> map=factory.getMapper(EventDataAnalysisMapper.class).queryCountEventForCorp(ddlist,codeList,getBeginTime(year),getEndTime(year));
                        if (map==null||!map.isEmpty()){
                            map=new LinkedHashMap<>();
                        }
                        map.put("deptCode",code);
                        map.put("deptName",district.getName());
                        msg.setData(map);
                    }
                }else{
                    //统计该水司的所有情况
                    List<String> codeList=new ArrayList<>();
                    List<DepartmentTreeBean> treeBeanList= (List<DepartmentTreeBean>) util.depttree(code,1).getData();
                    if(null!=treeBeanList&&treeBeanList.size()>0)
                        codeList.addAll(treeBeanList.stream().map(DepartmentTreeBean::getDepartmentCode).collect(Collectors.toList()));
                    LinkedHashMap<String,Object> map=factory.getMapper(EventDataAnalysisMapper.class).queryCountEventForCorp(ddlist,codeList,getBeginTime(year),getEndTime(year));
                    if (map==null||!map.isEmpty()){
                        map=new LinkedHashMap<>();
                    }
                    map.put("deptCode",code);
                    map.put("deptName",corpBean.getCorpName());
                    msg.setData(map);
                }
            }
            return msg;
        }catch (Exception e){
            logger.error(e.getMessage(),e);
            logger.error(e.getMessage(),e);
            msg.setCode(BusinessCode.EXCEPTION.getCode());
            msg.setDescription(BusinessCode.EXCEPTION.getDescription());
            return msg;
        }
    }

    @Override
    public MessageBean<?> queryListEvent(String code, Integer year) {
        MessageBean<List> msg=MessageBean.create(BusinessCode.SUCCESS.getCode(),BusinessCode.SUCCESS.getDescription(),List.class);
        try(SessionFactory factory=new SessionFactory()){
            if (null==year){
                msg.setCode(BusinessCode.EMPTY_PARAM_REQUIRED.getCode());
                msg.setDescription(BusinessCode.EMPTY_PARAM_REQUIRED.getDescription());
                return msg;
            }
            List<LinkedHashMap<String,Object>> maps=new ArrayList<>();
            List<DictData> ddlist = factory.getMapper(DictDataMapper.class).queryAll(null,"incident_type", null, null);
            if (null==ddlist||ddlist.size()<=0){
                return msg;
            }
            if (StringUtils.isEmpty(code)){
                //统计各片区下的情况
                //统计所有片区
                List<String> codeList=new ArrayList<>();
                List<District> districtList=districtService.queryAllDistrict(null,null).getData().getList();
                for(District district:districtList){
                    List<DistrictCorpRelation> relationList=districtService.queryDistrictCorp(district.getCode(),null,null).getData().getList();
                    if (null!=relationList&&relationList.size()>0)
                        codeList=relationList.stream().map(DistrictCorpRelation::getCorpCode).collect(Collectors.toList());
                    if (null!=codeList&&codeList.size()>0){
                       LinkedHashMap<String,Object> map=factory.getMapper(EventDataAnalysisMapper.class).queryCountEventForCorp(ddlist,codeList,getBeginTime(year),getEndTime(year));
                        if (map==null||!map.isEmpty()){
                            map=new LinkedHashMap<>();
                        }
                       map.put("deptCode",district.getCode());
                       map.put("deptName",district.getName());
                       maps.add(map);
                    }
                }
                msg.setData(maps);
            }else{
                //查询某个片区或者水司下的
                District district=factory.getMapper(DistrictMapper.class).queryOneDistrictByCode(code);
                CorpBean corpBean=factory.getMapper(CorpManagementMapper.class).get(code);
                if (null==district&&null==corpBean){
                    msg.setCode(BusinessCode.SUCCESS.getCode());
                    msg.setDescription(BusinessCode.SUCCESS.getDescription());
                    return msg;
                }
                if (null!=district){
                    //查询片区下的水司
                    List<String> codeList=new ArrayList<>();
                    List<DistrictCorpRelation> relationList=districtService.queryDistrictCorp(district.getCode(),null,null).getData().getList();
                    for(DistrictCorpRelation relation:relationList){
                        CorpBean corpBean1=factory.getMapper(CorpManagementMapper.class).get(relation.getCorpCode());
                        codeList.add(relation.getCorpCode());
                        LinkedHashMap<String,Object> map=factory.getMapper(EventDataAnalysisMapper.class).queryCountEventForCorp(ddlist,codeList,getBeginTime(year),getEndTime(year));
                        if (map==null||!map.isEmpty()){
                            map=new LinkedHashMap<>();
                        }
                        map.put("deptCode",district.getCode());
                        map.put("deptName",district.getName());
                        maps.add(map);
                    }
                    msg.setData(maps);
                }else{
                    //查询水司下的直属部门

                    List<DepartmentTreeBean> treeBeanList= (List<DepartmentTreeBean>) util.depttree(code,0).getData();
                    if (null!=treeBeanList&&treeBeanList.size()>0){
                        for(DepartmentTreeBean treeBean:treeBeanList){
                            List<String> codeList=new ArrayList<>();
                            List<DepartmentTreeBean> treeBeanList1=(List<DepartmentTreeBean>) util.depttree(code,1).getData();
                            if (null!=treeBeanList1&&treeBeanList1.size()>0){
                                codeList=treeBeanList1.stream().map(DepartmentTreeBean::getDepartmentCode).collect(Collectors.toList());
                            }
                            if (null!=codeList&&codeList.size()>0){
                                LinkedHashMap<String,Object> map=factory.getMapper(EventDataAnalysisMapper.class).queryCountEventForDepartmentCode(ddlist,codeList,getBeginTime(year),getEndTime(year));
                                if (map==null||!map.isEmpty()){
                                    map=new LinkedHashMap<>();
                                }
                                map.put("deptCode",treeBean.getDepartmentCode());
                                map.put("deptName",treeBean.getName());
                                maps.add(map);
                            }
                        }
                        msg.setData(maps);
                    }
                }
            }
            return msg;
        }catch (Exception e){
            logger.error(e.getMessage(),e);
            msg.setCode(BusinessCode.EXCEPTION.getCode());
            msg.setDescription(BusinessCode.EXCEPTION.getDescription());
            return msg;
        }
    }

    @Override
    public MessageBean<?> queryListEventGroupCorp(String code, Integer year) {
        MessageBean<List> msg=MessageBean.create(BusinessCode.SUCCESS.getCode(),BusinessCode.SUCCESS.getDescription(),List.class);
        try(SessionFactory factory=new SessionFactory()) {
            if (null==year){
                msg.setCode(BusinessCode.EMPTY_PARAM_REQUIRED.getCode());
                msg.setDescription(BusinessCode.EMPTY_PARAM_REQUIRED.getDescription());
                return msg;
            }
            List<LinkedHashMap<String,Object>> maps=new ArrayList<>();
            List<DictData> ddlist = factory.getMapper(DictDataMapper.class).queryAll(null,"incident_type", null, null);
            if (null==ddlist||ddlist.size()<=0){
                return msg;
            }
            if (StringUtils.isEmpty(code)){
                //统计所有公司
                //获取全部的水司
                List<CorpBean> corpBeanList=corpManagementService.corpBeanList();
                if (null!=corpBeanList&&corpBeanList.size()>0){
                    for(CorpBean corpBean:corpBeanList){
                        List<String> codeList=new ArrayList<>();
                        codeList.add(corpBean.getCorpCode());
                        LinkedHashMap<String,Object> map=factory.getMapper(EventDataAnalysisMapper.class).queryCountEventForCorp(ddlist,codeList,getBeginTime(year),getEndTime(year));
                        if (map==null||!map.isEmpty()){
                            map=new LinkedHashMap<>();
                        }
                        map.put("deptCode",corpBean.getCorpCode());
                        map.put("deptName",corpBean.getCorpName());
                        maps.add(map);
                    }
                }
                msg.setData(maps);
            }else{
                //查询水司下的直属部门
                List<DepartmentTreeBean> treeBeanList= (List<DepartmentTreeBean>) util.depttree(code,0).getData();
                if (null!=treeBeanList&&treeBeanList.size()>0){
                    for(DepartmentTreeBean treeBean:treeBeanList){
                        List<String> codeList=new ArrayList<>();
                        List<DepartmentTreeBean> treeBeanList1=(List<DepartmentTreeBean>) util.depttree(code,1).getData();
                        if (null!=treeBeanList1&&treeBeanList1.size()>0){
                            codeList=treeBeanList1.stream().map(DepartmentTreeBean::getDepartmentCode).collect(Collectors.toList());
                        }
                        if (null!=codeList&&codeList.size()>0){
                            LinkedHashMap<String,Object> map=factory.getMapper(EventDataAnalysisMapper.class).queryCountEventForDepartmentCode(ddlist,codeList,getBeginTime(year),getEndTime(year));
                            if (map==null||!map.isEmpty()){
                                map=new LinkedHashMap<>();
                            }
                            map.put("deptCode",treeBean.getDepartmentCode());
                            map.put("deptName",treeBean.getName());
                            maps.add(map);
                        }
                    }
                    msg.setData(maps);
                }
            }
            return msg;
        }catch (Exception e){
            logger.error(e.getMessage(),e);
            msg.setCode(BusinessCode.EXCEPTION.getCode());
            msg.setDescription(BusinessCode.EXCEPTION.getDescription());
            return msg;
        }
    }

    @Override
    public MessageBean<?> queryCountAccident(String code, Integer year) {
        MessageBean<Map> msg=MessageBean.create(BusinessCode.SUCCESS.getCode(),BusinessCode.SUCCESS.getDescription(),Map.class);
        try(SessionFactory factory=new SessionFactory()){
            if (null==year){
                msg.setCode(BusinessCode.EMPTY_PARAM_REQUIRED.getCode());
                msg.setDescription(BusinessCode.EMPTY_PARAM_REQUIRED.getDescription());
                return msg;
            }
            List<DictData> ddlist = factory.getMapper(DictDataMapper.class).queryAll(null,"accident_type", null, null);
            if (null==ddlist||ddlist.size()<=0){
                return msg;
            }
            if (StringUtils.isEmpty(code)){
                //查询整个集团的事件按类型统计情况
                EventDataAnalysisMapper dataAnalysisMapper=factory.getMapper(EventDataAnalysisMapper.class);
                LinkedHashMap<String,Object> map=dataAnalysisMapper.queryCountEventForCorp(ddlist,null,getBeginTime(year),getEndTime(year));
                if (map==null||!map.isEmpty()){
                    map=new LinkedHashMap<>();
                }
                map.put("集团","集团");
                msg.setData(map);
            }else{
                //统计某片区或者某个水司
                District district=factory.getMapper(DistrictMapper.class).queryOneDistrictByCode(code);
                CorpBean corpBean=factory.getMapper(CorpManagementMapper.class).get(code);
                if (null==district&&null==corpBean){
                    msg.setCode(BusinessCode.SUCCESS.getCode());
                    msg.setDescription(BusinessCode.SUCCESS.getDescription());
                    return msg;
                }
                if (null!=district){
                    List<String> codeList=new ArrayList<>();
                    List<DistrictCorpRelation> relationList=factory.getMapper(DistrictMapper.class).queryDistrictCorp(code,null,null);
                    if (null!=relationList&&relationList.size()>0)
                        codeList=relationList.stream().map(DistrictCorpRelation::getCorpCode).collect(Collectors.toList());
                    if (null!=codeList&&codeList.size()>0){
                        LinkedHashMap<String,Object> map=factory.getMapper(EventDataAnalysisMapper.class).queryCountAccidentForCorp(ddlist,codeList,getBeginTime(year),getEndTime(year));
                        if (map==null||!map.isEmpty()){
                            map=new LinkedHashMap<>();
                        }
                        map.put("deptCode",code);
                        map.put("deptName",district.getName());
                        msg.setData(map);
                    }
                }else{
                    //统计该水司的所有情况
                    List<String> codeList=new ArrayList<>();
                    List<DepartmentTreeBean> treeBeanList= (List<DepartmentTreeBean>) util.depttree(code,1).getData();
                    if(null!=treeBeanList&&treeBeanList.size()>0)
                        codeList.addAll(treeBeanList.stream().map(DepartmentTreeBean::getDepartmentCode).collect(Collectors.toList()));
                    LinkedHashMap<String,Object> map=factory.getMapper(EventDataAnalysisMapper.class).queryCountAccidentForCorp(ddlist,codeList,getBeginTime(year),getEndTime(year));
                    if (map==null||!map.isEmpty()){
                        map=new LinkedHashMap<>();
                    }
                    map.put("deptCode",code);
                    map.put("deptName",corpBean.getCorpName());
                    msg.setData(map);
                }
            }
            return msg;
        }catch (Exception e){
            logger.error(e.getMessage(),e);
            logger.error(e.getMessage(),e);
            msg.setCode(BusinessCode.EXCEPTION.getCode());
            msg.setDescription(BusinessCode.EXCEPTION.getDescription());
            return msg;
        }
    }

    @Override
    public MessageBean<?> queryListAccident(String code, Integer year) {
        MessageBean<List> msg=MessageBean.create(BusinessCode.SUCCESS.getCode(),BusinessCode.SUCCESS.getDescription(),List.class);
        try(SessionFactory factory=new SessionFactory()){
            if (null==year){
                msg.setCode(BusinessCode.EMPTY_PARAM_REQUIRED.getCode());
                msg.setDescription(BusinessCode.EMPTY_PARAM_REQUIRED.getDescription());
                return msg;
            }
            List<LinkedHashMap<String,Object>> maps=new ArrayList<>();
            List<DictData> ddlist = factory.getMapper(DictDataMapper.class).queryAll(null,"accident_type", null, null);
            if (null==ddlist||ddlist.size()<=0){
                return msg;
            }
            if (StringUtils.isEmpty(code)){
                //统计各片区下的情况
                //统计所有片区
                List<String> codeList=new ArrayList<>();
                List<District> districtList=districtService.queryAllDistrict(null,null).getData().getList();
                for(District district:districtList){
                    List<DistrictCorpRelation> relationList=districtService.queryDistrictCorp(district.getCode(),null,null).getData().getList();
                    if (null!=relationList&&relationList.size()>0)
                        codeList=relationList.stream().map(DistrictCorpRelation::getCorpCode).collect(Collectors.toList());
                    if (null!=codeList&&codeList.size()>0){
                        LinkedHashMap<String,Object> map=factory.getMapper(EventDataAnalysisMapper.class).queryCountAccidentForCorp(ddlist,codeList,getBeginTime(year),getEndTime(year));
                        if (map==null||!map.isEmpty()){
                            map=new LinkedHashMap<>();
                        }
                        map.put("deptCode",district.getCode());
                        map.put("deptName",district.getName());
                        maps.add(map);
                    }
                }
                msg.setData(maps);
            }else{
                //查询某个片区或者水司下的
                District district=factory.getMapper(DistrictMapper.class).queryOneDistrictByCode(code);
                CorpBean corpBean=factory.getMapper(CorpManagementMapper.class).get(code);
                if (null==district&&null==corpBean){
                    msg.setCode(BusinessCode.SUCCESS.getCode());
                    msg.setDescription(BusinessCode.SUCCESS.getDescription());
                    return msg;
                }
                if (null!=district){
                    //查询片区下的水司
                    List<String> codeList=new ArrayList<>();
                    List<DistrictCorpRelation> relationList=districtService.queryDistrictCorp(district.getCode(),null,null).getData().getList();
                    for(DistrictCorpRelation relation:relationList){
                        CorpBean corpBean1=factory.getMapper(CorpManagementMapper.class).get(relation.getCorpCode());
                        codeList.add(relation.getCorpCode());
                        LinkedHashMap<String,Object> map=factory.getMapper(EventDataAnalysisMapper.class).queryCountAccidentForCorp(ddlist,codeList,getBeginTime(year),getEndTime(year));
                        if (map==null||!map.isEmpty()){
                            map=new LinkedHashMap<>();
                        }
                        map.put("deptCode",district.getCode());
                        map.put("deptName",district.getName());
                        maps.add(map);
                    }
                    msg.setData(maps);
                }else{
                    //查询水司下的直属部门
                    List<DepartmentTreeBean> treeBeanList= (List<DepartmentTreeBean>) util.depttree(code,0).getData();
                    if (null!=treeBeanList&&treeBeanList.size()>0){
                        for(DepartmentTreeBean treeBean:treeBeanList){
                            List<String> codeList=new ArrayList<>();
                            List<DepartmentTreeBean> treeBeanList1=(List<DepartmentTreeBean>) util.depttree(code,1).getData();
                            if (null!=treeBeanList1&&treeBeanList1.size()>0){
                                codeList=treeBeanList1.stream().map(DepartmentTreeBean::getDepartmentCode).collect(Collectors.toList());
                            }
                            if (null!=codeList&&codeList.size()>0){
                                LinkedHashMap<String,Object> map=factory.getMapper(EventDataAnalysisMapper.class).queryCountAccidentForDepartmentCode(ddlist,codeList,getBeginTime(year),getEndTime(year));
                                if (map==null||!map.isEmpty()){
                                    map=new LinkedHashMap<>();
                                }
                                map.put("deptCode",treeBean.getDepartmentCode());
                                map.put("deptName",treeBean.getName());
                                maps.add(map);
                            }
                        }
                        msg.setData(maps);
                    }
                }
            }
            return msg;
        }catch (Exception e){
            logger.error(e.getMessage(),e);
            msg.setCode(BusinessCode.EXCEPTION.getCode());
            msg.setDescription(BusinessCode.EXCEPTION.getDescription());
            return msg;
        }
    }

    @Override
    public MessageBean<?> queryListAccidentGroupCorp(String code, Integer year) {
        MessageBean<List> msg=MessageBean.create(BusinessCode.SUCCESS.getCode(),BusinessCode.SUCCESS.getDescription(),List.class);
        try(SessionFactory factory=new SessionFactory()) {
            if (null==year){
                msg.setCode(BusinessCode.EMPTY_PARAM_REQUIRED.getCode());
                msg.setDescription(BusinessCode.EMPTY_PARAM_REQUIRED.getDescription());
                return msg;
            }
            List<LinkedHashMap<String,Object>> maps=new ArrayList<>();
            List<DictData> ddlist = factory.getMapper(DictDataMapper.class).queryAll(null,"accident_type", null, null);
            if (null==ddlist||ddlist.size()<=0){
                return msg;
            }
            if (StringUtils.isEmpty(code)){
                //统计所有公司
                //获取全部的水司
                List<CorpBean> corpBeanList=corpManagementService.corpBeanList();
                if (null!=corpBeanList&&corpBeanList.size()>0){
                    for(CorpBean corpBean:corpBeanList){
                        List<String> codeList=new ArrayList<>();
                        codeList.add(corpBean.getCorpCode());
                        LinkedHashMap<String,Object> map=factory.getMapper(EventDataAnalysisMapper.class).queryCountAccidentForCorp(ddlist,codeList,getBeginTime(year),getEndTime(year));
                        if (map==null||!map.isEmpty()){
                            map=new LinkedHashMap<>();
                        }
                        map.put("deptCode",corpBean.getCorpCode());
                        map.put("deptName",corpBean.getCorpName());
                        maps.add(map);
                    }
                }
                msg.setData(maps);
            }else{
                //查询水司下的直属部门
                List<DepartmentTreeBean> treeBeanList= (List<DepartmentTreeBean>) util.depttree(code,0).getData();
                if (null!=treeBeanList&&treeBeanList.size()>0){
                    for(DepartmentTreeBean treeBean:treeBeanList){
                        List<String> codeList=new ArrayList<>();
                        List<DepartmentTreeBean> treeBeanList1=(List<DepartmentTreeBean>) util.depttree(code,1).getData();
                        if (null!=treeBeanList1&&treeBeanList1.size()>0){
                            codeList=treeBeanList1.stream().map(DepartmentTreeBean::getDepartmentCode).collect(Collectors.toList());
                        }
                        if (null!=codeList&&codeList.size()>0){
                            LinkedHashMap<String,Object> map=factory.getMapper(EventDataAnalysisMapper.class).queryCountAccidentForDepartmentCode(ddlist,codeList,getBeginTime(year),getEndTime(year));
                            if (map==null||!map.isEmpty()){
                                map=new LinkedHashMap<>();
                            }
                            map.put("deptCode",treeBean.getDepartmentCode());
                            map.put("deptName",treeBean.getName());
                            maps.add(map);
                        }
                    }
                    msg.setData(maps);
                }
            }
            return msg;
        }catch (Exception e){
            logger.error(e.getMessage(),e);
            msg.setCode(BusinessCode.EXCEPTION.getCode());
            msg.setDescription(BusinessCode.EXCEPTION.getDescription());
            return msg;
        }
    }
    @Override
    public MessageBean<EventAnalysis> queryCountWorkTime(String code, String year) {
        MessageBean<EventAnalysis> msg=MessageBean.create(BusinessCode.SUCCESS.getCode(),BusinessCode.SUCCESS.getDescription(),EventAnalysis.class);
        try(SessionFactory factory=new SessionFactory()){
            if (null==year){
                msg.setCode(BusinessCode.EMPTY_PARAM_REQUIRED.getCode());
                msg.setDescription(BusinessCode.EMPTY_PARAM_REQUIRED.getDescription());
                return msg;
            }
            //将传入的年月转换成时间
//            String year1 = year.substring(0,4);
//            String month=year.substring(4,6);
//            int endMonth=Integer.parseInt(month)+1;
//            String endMonth2=endMonth<10?"0"+endMonth:endMonth+"";
            String startTimeStr=year+"-"+01+"-"+"01"+" "+"00:00:00";
            String endTimeStr=year+"-"+12+"-"+"31"+" "+"23:59:59";
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date startTime=sdf.parse(startTimeStr);
            Date endTime=sdf.parse(endTimeStr);

            if (StringUtils.isEmpty(code)){
                //统计全集团
                EventAnalysis analysis=factory.getMapper(EventDataAnalysisMapper.class).queryCountWorkTimeForCorp(null,year);
                if (null==analysis){
                    analysis=new EventAnalysis();
                }
                //从事件管理统计损工时
                BigDecimal eventLossHours=factory.getMapper(EventMapper.class).countEventSumLossHour(null,startTime,endTime);
                //从事故报告统计损工时
                BigDecimal accidentLossHour=factory.getMapper(AccidentReportMapper.class).countAccidentSumLossHour(null,startTime,endTime);

                BigDecimal total=new BigDecimal(0);
                if (null!=eventLossHours){
                    if (null!=accidentLossHour){
                        total=eventLossHours.add(accidentLossHour);
                    }else{
                        total=eventLossHours;
                    }
                }else{
                    if (null!=accidentLossHour){
                        total=accidentLossHour;
                    }
                }
                analysis.setLossWorkTimesCount(total);
                analysis.setDepartmentName("集团");
                msg.setData(analysis);
                return msg;
            }else{
                //统计某片区或者某个水司
                District district=factory.getMapper(DistrictMapper.class).queryOneDistrictByCode(code);
                CorpBean corpBean=factory.getMapper(CorpManagementMapper.class).get(code);
                if (null==district&&null==corpBean){
                    msg.setCode(BusinessCode.SUCCESS.getCode());
                    msg.setDescription(BusinessCode.SUCCESS.getDescription());
                    return msg;
                }
                List<String> codeList=new ArrayList<>();
                if (null!=district){
                    //片区
                    List<DistrictCorpRelation> relationList=factory.getMapper(DistrictMapper.class).queryDistrictCorp(code,null,null);
                    for(DistrictCorpRelation relation:relationList){
                        List<DepartmentTreeBean> treeBeanList= (List<DepartmentTreeBean>) util.depttree(relation.getCorpCode(),1).getData();
                        if (null!=treeBeanList&&treeBeanList.size()>0)
                            codeList.addAll(treeBeanList.stream().map(DepartmentTreeBean::getDepartmentCode).collect(Collectors.toList()));
                        EventAnalysis analysis=factory.getMapper(EventDataAnalysisMapper.class).queryCountWorkTimeForCorp(codeList,year);
                        if (null==analysis){
                            analysis=new EventAnalysis();
                        }
                        //从事件管理统计损工时
                        BigDecimal eventLossHours=factory.getMapper(EventMapper.class).countEventSumLossHour(codeList,startTime,endTime);
                        //从事故报告统计损工时
                        BigDecimal accidentLossHour=factory.getMapper(AccidentReportMapper.class).countAccidentSumLossHour(codeList,startTime,endTime);

                        BigDecimal total=new BigDecimal(0);
                        if (null!=eventLossHours){
                            if (null!=accidentLossHour){
                                total=eventLossHours.add(accidentLossHour);
                            }else{
                                total=eventLossHours;
                            }
                        }else{
                            if (null!=accidentLossHour){
                                total=accidentLossHour;
                            }
                        }
                        analysis.setLossWorkTimesCount(total);
                        analysis.setDepartmentCode(code);
                        analysis.setDepartmentName(district.getName());
                        msg.setData(analysis);
                    }
                    return msg;
                }else{
                    //水司
                    List<DepartmentTreeBean> treeBeanList= (List<DepartmentTreeBean>) util.depttree(code,1).getData();
                    if(null!=treeBeanList&&treeBeanList.size()>0)
                    codeList.addAll(treeBeanList.stream().map(DepartmentTreeBean::getDepartmentCode).collect(Collectors.toList()));
                    EventAnalysis analysis=factory.getMapper(EventDataAnalysisMapper.class).queryCountWorkTimeForCorp(codeList,year);
                    if (null==analysis){
                        analysis=new EventAnalysis();
                    }
                    //从事件管理统计损工时
                    BigDecimal eventLossHours=factory.getMapper(EventMapper.class).countEventSumLossHour(codeList,startTime,endTime);
                    //从事故报告统计损工时
                    BigDecimal accidentLossHour=factory.getMapper(AccidentReportMapper.class).countAccidentSumLossHour(codeList,startTime,endTime);

                    BigDecimal total=new BigDecimal(0);
                    if (null!=eventLossHours){
                        if (null!=accidentLossHour){
                            total=eventLossHours.add(accidentLossHour);
                        }else{
                            total=eventLossHours;
                        }
                    }else{
                        if (null!=accidentLossHour){
                            total=accidentLossHour;
                        }
                    }
                    analysis.setLossWorkTimesCount(total);
                    analysis.setDepartmentCode(code);
                    analysis.setDepartmentName(corpBean.getCorpName());
                    msg.setData(analysis);
                    return msg;
                }
            }
        }catch (Exception e){
            logger.error(e.getMessage(),e);
            msg.setCode(BusinessCode.EXCEPTION.getCode());
            msg.setDescription(BusinessCode.EXCEPTION.getDescription());
            return msg;
        }
    }

    @Override
    public MessageBean<List<EventAnalysis>> queryListWorkTime(String code, String year) {
        MessageBean<List<EventAnalysis>> msg=new MessageBean<>();
        try(SessionFactory factory=new SessionFactory()) {
            if (null==year){
                msg.setCode(BusinessCode.EMPTY_PARAM_REQUIRED.getCode());
                msg.setDescription(BusinessCode.EMPTY_PARAM_REQUIRED.getDescription());
                return msg;
            }
            //将传入的年月转换成时间
//            String year1 = year.substring(0,4);
//            String month=year.substring(4,6);
//            int endMonth=Integer.parseInt(month)+1;
//            String endMonth2=endMonth<10?"0"+endMonth:endMonth+"";
            String startTimeStr=year+"-"+01+"-"+"01"+" "+"00:00:00";
            String endTimeStr=year+"-"+12+"-"+"31"+" "+"23:59:59";
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date startTime=sdf.parse(startTimeStr);
            Date endTime=sdf.parse(endTimeStr);
            List<EventAnalysis> analysisList=new ArrayList<>();
            if (StringUtils.isEmpty(code)){
                //统计所有片区
                List<District> districtList=districtService.queryAllDistrict(null,null).getData().getList();
                for(District district:districtList){
                    List<DistrictCorpRelation> relationList=districtService.queryDistrictCorp(district.getCode(),null,null).getData().getList();
                    List<String> codeList=new ArrayList<>();
                    for(DistrictCorpRelation relation:relationList){
                        List<DepartmentTreeBean> treeBeanList= (List<DepartmentTreeBean>) util.depttree(relation.getCorpCode(),1).getData();
                        if (null!=treeBeanList&&treeBeanList.size()>0)
                            codeList.addAll(treeBeanList.stream().map(DepartmentTreeBean::getDepartmentCode).collect(Collectors.toList()));
                    }
                    if (null!=codeList&&codeList.size()>0){
                        EventAnalysis analysis=factory.getMapper(EventDataAnalysisMapper.class).queryCountWorkTimeForCorp(codeList,year);
                        if (null==analysis){
                            analysis=new EventAnalysis();
                        }
                        //从事件管理统计损工时
                        BigDecimal eventLossHours=factory.getMapper(EventMapper.class).countEventSumLossHour(codeList,startTime,endTime);
                        //从事故报告统计损工时
                        BigDecimal accidentLossHour=factory.getMapper(AccidentReportMapper.class).countAccidentSumLossHour(codeList,startTime,endTime);

                        BigDecimal total=new BigDecimal(0);
                        if (null!=eventLossHours){
                            if (null!=accidentLossHour){
                                total=eventLossHours.add(accidentLossHour);
                            }else{
                                total=eventLossHours;
                            }
                        }else{
                            if (null!=accidentLossHour){
                                total=accidentLossHour;
                            }
                        }
                        analysis.setLossWorkTimesCount(total);
                        analysis.setDepartmentCode(district.getCode());
                        analysis.setDepartmentName(district.getName());
                        analysisList.add(analysis);
                    }
                }
                msg.setData(analysisList);
                msg.setCode(BusinessCode.SUCCESS.getCode());
                msg.setDescription(BusinessCode.SUCCESS.getDescription());
                return msg;
            }else{
                //统计某个片区下的水司
                District district=districtService.queryOneByCode(code).getData();
                if (null!=district){
                    //查询该片区下的各水司列表
                    List<DistrictCorpRelation> relationList=districtService.queryDistrictCorp(district.getCode(),null,null).getData().getList();
                    for(DistrictCorpRelation relation:relationList){
                        CorpBean corpBean=factory.getMapper(CorpManagementMapper.class).get(relation.getCorpCode());
                        List<String> codeList=new ArrayList<>();
                        codeList.add(relation.getCorpCode());
                        EventAnalysis analysis=factory.getMapper(EventDataAnalysisMapper.class).queryCountWorkTimeForCorp(codeList,year);
                        if (null==analysis){
                            analysis=new EventAnalysis();
                        }
                        //从事件管理统计损工时
                        BigDecimal eventLossHours=factory.getMapper(EventMapper.class).countEventSumLossHour(codeList,startTime,endTime);
                        //从事故报告统计损工时
                        BigDecimal accidentLossHour=factory.getMapper(AccidentReportMapper.class).countAccidentSumLossHour(codeList,startTime,endTime);

                        BigDecimal total=new BigDecimal(0);
                        if (null!=eventLossHours){
                            if (null!=accidentLossHour){
                                total=eventLossHours.add(accidentLossHour);
                            }else{
                                total=eventLossHours;
                            }
                        }else{
                            if (null!=accidentLossHour){
                                total=accidentLossHour;
                            }
                        }
                        analysis.setLossWorkTimesCount(total);
                        analysis.setDepartmentCode(district.getCode());
                        analysis.setDepartmentName(district.getName());
                        analysisList.add(analysis);
                    }
                    msg.setData(analysisList);
                    msg.setCode(BusinessCode.SUCCESS.getCode());
                    msg.setDescription(BusinessCode.SUCCESS.getDescription());
                    return msg;
                }else{
                    CorpBean corpBean=factory.getMapper(CorpManagementMapper.class).get(code);
                    if (null==corpBean){
                        msg.setCode(BusinessCode.SUCCESS.getCode());
                        msg.setDescription(BusinessCode.SUCCESS.getDescription());
                        return msg;
                    }
                    List<String> codeList=new ArrayList<>();
                    List<DepartmentTreeBean> treeBeanList= (List<DepartmentTreeBean>) util.depttree(code,0).getData();
                    if (null!=treeBeanList&&treeBeanList.size()>0){
                        for(DepartmentTreeBean treeBean:treeBeanList){
                            List<DepartmentTreeBean> treeBeanList1=(List<DepartmentTreeBean>) util.depttree(code,1).getData();
                            if (null!=treeBeanList1&&treeBeanList1.size()>0){
                                codeList=treeBeanList1.stream().map(DepartmentTreeBean::getDepartmentCode).collect(Collectors.toList());
                            }
                            EventAnalysis analysis=new EventAnalysis();
                            if (null!=codeList&&codeList.size()>0){
                                analysis=factory.getMapper(EventDataAnalysisMapper.class).queryCountWorkTimeForDepartmentCode(codeList,year);
                            }
                            if (null==analysis){
                                analysis=new EventAnalysis();
                            }
                            //从事件管理统计损工时
                            BigDecimal eventLossHours=factory.getMapper(EventMapper.class).countEventSumLossHour(codeList,startTime,endTime);
                            //从事故报告统计损工时
                            BigDecimal accidentLossHour=factory.getMapper(AccidentReportMapper.class).countAccidentSumLossHour(codeList,startTime,endTime);

                            BigDecimal total=new BigDecimal(0);
                            if (null!=eventLossHours){
                                if (null!=accidentLossHour){
                                    total=eventLossHours.add(accidentLossHour);
                                }else{
                                    total=eventLossHours;
                                }
                            }else{
                                if (null!=accidentLossHour){
                                    total=accidentLossHour;
                                }
                            }
                            analysis.setLossWorkTimesCount(total);
                            analysis.setDepartmentCode(treeBean.getDepartmentCode());
                            analysis.setDepartmentName(treeBean.getName());
                            analysisList.add(analysis);
                        }
                    }
                    msg.setData(analysisList);
                    msg.setCode(BusinessCode.SUCCESS.getCode());
                    msg.setDescription(BusinessCode.SUCCESS.getDescription());
                    return msg;
                }
            }
        }catch (Exception e){
            logger.error(e.getMessage(),e);
            msg.setCode(BusinessCode.EXCEPTION.getCode());
            msg.setDescription(BusinessCode.EXCEPTION.getDescription());
            return msg;
        }
    }

    @Override
    public MessageBean<List<EventAnalysis>> queryListWorkTimeGroupCorp(String code, String year) {
        MessageBean<List<EventAnalysis>> msg=new MessageBean<>();
        try(SessionFactory factory=new SessionFactory()){
            if (null==year){
                msg.setCode(BusinessCode.EMPTY_PARAM_REQUIRED.getCode());
                msg.setDescription(BusinessCode.EMPTY_PARAM_REQUIRED.getDescription());
                return msg;
            }
            //将传入的年月转换成时间
//            String year1 = year.substring(0,4);
//            String month=year.substring(4,6);
//            int endMonth=Integer.parseInt(month)+1;
//            String endMonth2=endMonth<10?"0"+endMonth:endMonth+"";
            String startTimeStr=year+"-"+01+"-"+"01"+" "+"00:00:00";
            String endTimeStr=year+"-"+12+"-"+"31"+" "+"23:59:59";
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date startTime=sdf.parse(startTimeStr);
            Date endTime=sdf.parse(endTimeStr);
            List<EventAnalysis> analysisList=new ArrayList<>();
            if (StringUtils.isEmpty(code)){
                //获取全部的水司
                List<CorpBean> corpBeanList=corpManagementService.corpBeanList();
                if (null!=corpBeanList&&corpBeanList.size()>0){
                    for(CorpBean corpBean:corpBeanList){
                        List<String> codeList=new ArrayList<>();
                        codeList.add(corpBean.getCorpCode());
                        EventAnalysis analysis=factory.getMapper(EventDataAnalysisMapper.class).queryCountWorkTimeForCorp(codeList,year);
                        if (null==analysis){
                            analysis=new EventAnalysis();
                        }
                        //从事件管理统计损工时
                        BigDecimal eventLossHours=factory.getMapper(EventMapper.class).countEventSumLossHour(codeList,startTime,endTime);
                        //从事故报告统计损工时
                        BigDecimal accidentLossHour=factory.getMapper(AccidentReportMapper.class).countAccidentSumLossHour(codeList,startTime,endTime);

                        BigDecimal total=new BigDecimal(0);
                        if (null!=eventLossHours){
                            if (null!=accidentLossHour){
                                total=eventLossHours.add(accidentLossHour);
                            }else{
                                total=eventLossHours;
                            }
                        }else{
                            if (null!=accidentLossHour){
                                total=accidentLossHour;
                            }
                        }
                        analysis.setLossWorkTimesCount(total);
                        analysis.setDepartmentCode(corpBean.getCorpCode());
                        analysis.setDepartmentName(corpBean.getCorpName());
                        analysisList.add(analysis);
                    }
                }
                msg.setCode(BusinessCode.SUCCESS.getCode());
                msg.setDescription(BusinessCode.SUCCESS.getDescription());
                msg.setData(analysisList);
                return msg;
            }else{
                //
                CorpBean corpBean=factory.getMapper(CorpManagementMapper.class).get(code);
                if (null==corpBean){
                    msg.setCode(BusinessCode.SUCCESS.getCode());
                    msg.setDescription(BusinessCode.SUCCESS.getDescription());
                    return msg;
                }
                List<String> codeList=new ArrayList<>();
                List<DepartmentTreeBean> treeBeanList= (List<DepartmentTreeBean>) util.depttree(code,0).getData();
                if (null!=treeBeanList&&treeBeanList.size()>0){
                    for(DepartmentTreeBean treeBean:treeBeanList){
                        List<DepartmentTreeBean> treeBeanList1=(List<DepartmentTreeBean>) util.depttree(code,1).getData();
                        if (null!=treeBeanList1&&treeBeanList1.size()>0){
                            codeList=treeBeanList1.stream().map(DepartmentTreeBean::getDepartmentCode).collect(Collectors.toList());
                        }
                        EventAnalysis analysis=new EventAnalysis();
                        if (null!=codeList&&codeList.size()>0){
                            analysis=factory.getMapper(EventDataAnalysisMapper.class).queryCountWorkTimeForDepartmentCode(codeList,year);
                        }
                        if (null==analysis){
                            analysis=new EventAnalysis();
                        }
                        //从事件管理统计损工时
                        BigDecimal eventLossHours=factory.getMapper(EventMapper.class).countEventSumLossHour(codeList,startTime,endTime);
                        //从事故报告统计损工时
                        BigDecimal accidentLossHour=factory.getMapper(AccidentReportMapper.class).countAccidentSumLossHour(codeList,startTime,endTime);

                        BigDecimal total=new BigDecimal(0);
                        if (null!=eventLossHours){
                            if (null!=accidentLossHour){
                                total=eventLossHours.add(accidentLossHour);
                            }else{
                                total=eventLossHours;
                            }
                        }else{
                            if (null!=accidentLossHour){
                                total=accidentLossHour;
                            }
                        }
                        analysis.setLossWorkTimesCount(total);
                        analysis.setDepartmentCode(treeBean.getDepartmentCode());
                        analysis.setDepartmentName(treeBean.getName());
                        analysisList.add(analysis);
                    }
                }
                msg.setCode(BusinessCode.SUCCESS.getCode());
                msg.setDescription(BusinessCode.SUCCESS.getDescription());
                msg.setData(analysisList);
                return msg;
            }
        }catch (Exception e){
            logger.error(e.getMessage(),e);
            msg.setCode(BusinessCode.EXCEPTION.getCode());
            msg.setDescription(BusinessCode.EXCEPTION.getDescription());
            return msg;
        }
    }


    /**
     * 获取某年的开始日期
     * @param year
     * @return
     */
    private static Date getBeginTime(int year) {
        YearMonth yearMonth = YearMonth.of(year, 1);
        LocalDate localDate = yearMonth.atDay(1);
        LocalDateTime startOfDay = localDate.atStartOfDay();
        ZonedDateTime zonedDateTime = startOfDay.atZone(ZoneId.systemDefault());
        return Date.from(zonedDateTime.toInstant());
    }

    /**
     * 获取某年的结束日期
     * @param year
     * @return
     */
    private static Date getEndTime(int year) {
        YearMonth yearMonth = YearMonth.of(year, 12);
        LocalDate endOfMonth = yearMonth.atEndOfMonth();
        LocalDateTime localDateTime = endOfMonth.atTime(23, 59, 59, 999);
        ZonedDateTime zonedDateTime = localDateTime.atZone(ZoneId.systemDefault());
        return Date.from(zonedDateTime.toInstant());
    }

}
